import { loadUserSummerCache } from "./storage";

export default {
  // 消息类型
  TEXT_MSG: 1,
  RECALL_MSG: 2,
  EMOJI_MSG: 3,
  IMG_MSG: 4,
  FILE_MSG: 5,
  SOUND_MSG: 6,
  VIDEO_MSG: 7,

  /**
   *  解析消息回复内容
   *
   * @param msgInfo
   */
  parseReplyContent(msgInfo) {
    console.log(msgInfo)
    // 更具不同消息类型解析内容
    let content = "";
    if (msgInfo.type === this.TEXT_MSG) {
      // 解析html文本消息
      const parser = new DOMParser();
      const doc = parser.parseFromString(msgInfo.body.content,"text/html");
      const atElList = doc.body.getElementsByClassName("at-class")
      for (let i = 0; i < atElList.length; i++) {
        content += atElList[i].innerText;
      }
      content += " " + doc.body.lastChild.textContent
    } else if (msgInfo.type === this.IMG_MSG) {
      // 图片消息
      content = " 【图片】";
    } else if (msgInfo.type === this.FILE_MSG) {
      // 文件消息
      content = " 【文件】";
    } else if (msgInfo.type === this.EMOJI_MSG) {
      // 表情包
      content = " 【表情包】"
    }
    return content;
  },

  /**
   *  构建文本消息请求体
   *
   * @param roomId 房间号
   * @param content 消息内容
   * @param replyMsgId 回复消息的ID
   * @param atUidList at列表
   */
  buildTextMsgReqBody(roomId, content, replyMsgId, atUidList) {
    return {
      roomId,
      msgType: this.TEXT_MSG,
      body: {
        content,
        replyMsgId,
        atUidList,
      },
    };
  },

  /**
   *  构建撤回消息请求体
   *
   * @param roomId 房间号
   * @param msgId 需要撤回的消息ID
   * @param recallTime 撤回时间
   * @returns {{msgType: number, body: {recallTime, msgId}, roomId}}
   */
  buildRecallMsgReqBody(roomId, msgId, recallTime) {
    return {
      roomId,
      msgType: this.RECALL_MSG,
      body: {
        msgId,
        recallTime
      }
    }
  },

  /**
   *  构建图片消息请求实体
   *
   * @param roomId 房间号
   * @param url 图片url
   * @param width 图片宽度
   * @param height 高度
   * @param size 大小
   * @return {{msgType: number, body: {size, width, url, height}, roomId}}
   */
  buildImgMsgReqBody(roomId, url, width, height, size) {
    return {
      roomId,
      msgType: this.IMG_MSG,
      body: {
        url,
        width,
        height,
        size,
      },
    };
  },

  /**
   *  构建文件消息请求实体
   *
   * @param roomId 房间号
   * @param url 文件地址
   * @param fileName 文件名
   * @param size 文件大小
   * @return {{msgType: number, body: {fileName, size, url}, roomId}}
   */
  buildFileMsgReqBody(roomId, url, fileName, size) {
    return {
      roomId,
      msgType: this.FILE_MSG,
      body: {
        url,
        fileName,
        size,
      },
    };
  },

  buildImgEmojiMsgReqBody(roomId, url) {
    return {
      roomId,
      msgType: this.EMOJI_MSG,
      body: {
        url,
      },
    };
  },

  /**
   *  构建消息响应体
   *
   * @param msgBody
   * @param type
   */
  buildMsgRespBody(msgBody, type) {
    let body = null;
    switch (type) {
      case this.TEXT_MSG:
        body = this.buildTextMsgRespBody(msgBody);
        break;
      case this.IMG_MSG:
        body = this.buildImgMsgRespBody(msgBody);
        break;
      case this.FILE_MSG:
        body = this.buildFileMsgRespBody(msgBody);
        break;
      case this.EMOJI_MSG:
        body = this.buildImgEmojiMsgRespBody(msgBody);
        break;
      case this.RECALL_MSG:
        body = this.buildRecallMsgRespBody(msgBody);
        break;
    }
    return body;
  },

  /**
   *  构建文本消息响应体
   *
   * @param msgBody
   */
  buildTextMsgRespBody(msgBody) {
    let body = null;
    if (msgBody.reply) {
      // 有回复消息,构建回复体用户信息
      let userInfo = loadUserSummerCache(msgBody.reply.uid);
      body = msgBody.reply.body;
      Object.assign(body, userInfo);
      msgBody.reply.body = body;
    }

    return msgBody;
  },

  /**
   *  构建图片消息
   *
   * @param msgBody
   */
  buildImgMsgRespBody(msgBody) {
    return msgBody;
  },

  /**
   * 构建文件消息响应实体
   *
   * @param msgBody
   * @return {*}
   */
  buildFileMsgRespBody(msgBody) {
    return msgBody;
  },

  buildImgEmojiMsgRespBody(msgBody) {
    return msgBody;
  },

  buildRecallMsgRespBody(msgBody) {
    return msgBody;
  }
};
